//表格样式模块  查询数据的展示
//=========================================================
var IMGDOMAIN = "../../images/";


var table_options = {'url': {}, 'cols': {}};
//------------------------------------------------------
var row_numbers = {title: '序号', type: "numbers", fixed: "left"};

//=====================================================

table_options.url.admin = backstage_domain + "/superadmin/getuserinpageby_";

table_options.cols.admin = [[ //表头
    row_numbers,
    {field: 'id', title: 'ID', hide: true},
    {field: 'username', title: '用户名', align: "center"},
    {field: 'name', title: '名字', align: "center"},
    {
        field: 'roles',
        title: '角色',
        align: "center",
        templet: function (re) {
            let html = '';
            for (let role of re.roles) {
                html += "，" + role;
            }
            return html.substring(1);
        }
    },
    {
        field: 'authorities',
        title: '权限',
        align: "center",
        templet: function (re) {
            let html = '';
            for (let authority of re.authorities) {
                html += "，" + authority;
            }
            return html.substring(1);
        }
    },
    {
        field: 'survival',
        title: '是否可用',
        align: "center",
        templet: function (d) {
            var html1 = '<input  lay-filter="user_survival" type="checkbox" name="survival" lay-skin="switch" lay-text="可用|停用" checked value="' + d.id + '">';
            var html0 = '<input lay-filter="user_survival" type="checkbox" name="survival" lay-skin="switch" lay-text="可用|停用" value="' + d.id + '">';
            return d.survival == 1 ? html1 : html0;
        }
    },
    {
        title: '操作',
        fixed: 'right',
        width: 150,
        align: 'center',
        toolbar: '<div><button class="layui-btn layui-btn-sm" lay-event="user_update" >编辑</button></div>'
    }
]];

//====================================================
var tableSwitch = {};
//
tableSwitch.admin = function () {
    form.on('switch(user_survival)', function (data) {
        var updatedata = {};
        updatedata.id = data.value;
        if (data.elem.checked)
            updatedata.survival = 1;
        else
            updatedata.survival = 0;
        myajax('/superadmin/updateusersurvival', 'post', updatedata);
    });
};

//====================================================

table_options.url.role = backstage_domain + "/role/getroleinpageby_";

table_options.cols.role = [[ //表头
    row_numbers,
    {field: 'id', title: 'ID', hide: true},
    {field: 'role', title: '角色', align: "center"},
    {field: 'introduction', title: '说明', align: "center"},
    {
        field: 'authorities',
        title: '权限',
        align: "center",
        templet: function (re) {
            let html = '';
            for (let authority of re.authorities) {
                html += "，" + authority;
            }
            return html.substring(1);
        }
    },
    {
        title: '操作',
        fixed: 'right',
        width: 150,
        align: 'center',
        toolbar: '<div><button class="layui-btn layui-btn-sm" lay-event="role_update" >编辑</button><button class="layui-btn  layui-btn-danger layui-btn-sm" lay-event="role_delete" >删除</button></div>'
    }
]];

//====================================================

table_options.url.authority = backstage_domain + "/authority/getauthorityinpageby_";

table_options.cols.authority = [[ //表头
    row_numbers,
    {field: 'id', title: 'ID', hide: true},
    {field: 'authority', title: '权限名', align: "center"},
    {field: 'introduction', title: '说明', align: "center"},

    {
        title: '操作',
        fixed: 'right',
        width: 150,
        align: 'center',
        toolbar: '<div><button class="layui-btn layui-btn-sm" lay-event="authority_update" >编辑</button><button class="layui-btn  layui-btn-danger layui-btn-sm" lay-event="authority_delete" >删除</button></div>'
    }
]];

//====================================================

table_options.url.selfreport = backstage_domain + "/dailyreport/getselfdailyreport";

table_options.cols.selfreport = [[ //表头
    row_numbers,
    {field: 'id', title: 'ID', hide: true},
    {field: 'userClass', title: '专业', align: "center"},
    {field: 'classTeacherName', title: '督导', align: "center"},
    {field: 'teacherName', title: '教练', align: "center"},
    {
        field: 'status',
        title: '日报状态',
        align: "center",
        templet: function (re) {
            let html = '';
            switch (re.status) {
                case 0:
                    html = "草稿";
                    break;
                case 1:
                    html = "已提交";
                    break;
                case 2:
                    html = "已查看";
                    break;
            }
            return html;
        }
    },
    {field: 'createtime', title: '创建日期', align: "center"},
    {field: 'dailyId', title: '日报ID', hide: true},
    {
        title: '查看日报内容',
        fixed: 'right',
        width: 150,
        align: 'center',
        toolbar: '<div><button class="layui-btn layui-btn-sm" lay-event="selfreport_show" >查看</button></div>'
    }
]];


//==========================================

table_options.url.adminreport = backstage_domain + "/dailyreport/getadminreport";

table_options.cols.adminreport = [[ //表头
    row_numbers,
    {field: 'id', title: 'ID', hide: true},
    {field: 'userClass', title: '专业', align: "center"},
    {field: 'classTeacherName', title: '督导', align: "center"},
    {field: 'teacherName', title: '教练', align: "center"},
    {
        field: 'status',
        title: '日报状态',
        align: "center",
        templet: function (re) {
            return re.status == 1 ? "已提交" : "已查看";
        }
    },
    {field: 'submittime', title: '提交日期', align: "center"},
    {field: 'dailyId', title: '日报ID', hide: true},
    {
        title: '查看日报内容',
        fixed: 'right',
        width: 150,
        align: 'center',
        toolbar: '<div><button class="layui-btn layui-btn-sm" lay-event="adminreport_show" >查看</button></div>'
    }
]];

//==========================================

table_options.url.word = backstage_domain + "/wxword/getwordpageby_";

table_options.cols.word = [[ //表头
    row_numbers,
    {field: 'id', title: 'ID', hide: true},
    {field: 'word', title: '单词', align: "center"},
    {field: 'wordexplain', title: '释义', align: "center"},
    {
        title: '点赞',
        align: "center",
        templet: function (re) {
            var html = '';
            if (re.supportflag) {
                html = '<button  flag="' + re.supportflag + '" wordid="' + re.id +
                    '" onclick="wordsupport(this)" class="layui-icon" style="font-size: 20px;border: 0;color: #009E94">&#xe6c6;<span>' + re.supportnum + '</span></button>';
            } else {
                html = '<button  flag="' + re.supportflag + '" wordid="' + re.id +
                    '" onclick="wordsupport(this)" class="layui-icon" style="font-size: 20px;border: 0">&#xe6c6;<span>' + re.supportnum + '</span></button>';
            }
            return html;
        }
    },
    {
        title: '收藏',
        align: "center",
        templet: function (re) {
            var html = '';
            if (re.collectionflag) {
                html = '<button  flag="' + re.collectionflag + '" wordid="' + re.id +
                    '" onclick="wordcollection(this)" class="layui-icon" style="font-size: 20px;border: 0;color: #009E94">&#xe600;<span>' + re.collectionnum + '</span></button>';
            } else {
                html = '<button  flag="' + re.collectionflag + '" wordid="' + re.id +
                    '" onclick="wordcollection(this)" class="layui-icon" style="font-size: 20px;border: 0">&#xe600;<span>' + re.collectionnum + '</span></button>';
            }
            return html;
        }
    }
]];

function wordsupport(obj) {
    var word_id = $(obj).attr('wordid');
    var updateflag = 0;
    if ($(obj).attr('flag') == "true") {
        $(obj).css('color', "");
        $(obj).attr('flag', "false");
        let span = $(obj).children("span");
        span.text(parseInt(span.text()) - 1);
    } else {
        $(obj).css('color', "#009E94");
        $(obj).attr('flag', "true");
        let span = $(obj).children("span");
        span.text(parseInt(span.text()) + 1);
        updateflag = 1;
    }
    myajax('/wordsupport/updatewordsupport', 'post', {flag: updateflag, word_id: word_id},function (res) {
        if(res.status!=200){
            layer.msg("更新失败");
        }
    },function () {
        layer.msg("网络错误");
    })

}

function wordcollection(obj) {
    var word_id = $(obj).attr('wordid');
    var updateflag = 0;
    if ($(obj).attr('flag') == "true") {
        $(obj).css('color', "");
        $(obj).attr('flag', "false");
        let span = $(obj).children("span");
        span.text(parseInt(span.text()) - 1);
    } else {
        $(obj).css('color', "#009E94");
        $(obj).attr('flag', "true");
        let span = $(obj).children("span");
        span.text(parseInt(span.text()) + 1);
        updateflag = 1;
    }
    myajax('/usercollection/updateusercollection', 'post', {flag: updateflag, word_id: word_id},function (res) {
        if(res.status!=200){
            layer.msg("更新失败");
        }
    },function () {
        layer.msg("网络错误");
    })
}

//---------------------------------------------------------
function get_render_options(id) {
    return {
        elem: "#" + id, //指定原始表格元素选择器（推荐id选择器）
        url: table_options.url[id],
        response: {
            statusName: 'status',//规定数据状态的字段名称，默认：code
            statusCode: 200 //规定成功的状态码，默认：0
        },
        page: true,
        limit: 10,
        limits: [10, 15, 20, 25, 30, 35, 40],
        defaultToolbar: [],
        toolbar: "#table_options_toolbar_html_" + id,
        cols: table_options.cols[id],
        id: id
        //,…… //更多参数参考右侧目录：基本参数选项
    }
}


//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//初始化表格头
var initTable = {};
initTable.admin = function () {
    $("#admin_role").empty();
    $("#admin-role").empty();
    $("#admin_authority").empty();
    myajax("/role/getallrole", "get", {}, function (result) {
        if (result.status == 200) {
            let html1 = '';
            let html2 = '';
            for (let role of result.data) {
                html1 += '<button type="button" class="layui-btn layui-btn-primary layui-btn-sm my-select-checkbox admin_role" onclick="checkedShow(this)" value="' + role.flag + '">' + role.role + '</button>';
                html2 += '<input id="ROLE_update_' + role.role + '" type="checkbox"  lay-filter="admin_update"  name="roles" title="' + role.role + '" value="' + role.role + '"  role_id="' + role.id + '" />';
            }
            $("#admin_role").append(html1);
            $("#admin-role").append(html2);
        }
    }, function () {
        layer.msg("网络错误");
    }, false);
    myajax("/authority/getallauthority", "get", {}, function (result) {
        if (result.status == 200) {
            let html = '';
            for (let authority of result.data) {
                html += '<button type="button" class="layui-btn layui-btn-primary layui-btn-sm my-select-checkbox admin_authority" onclick="checkedShow(this)" value="' + authority.flag + '">' + authority.authority + '</button>';
            }
            $("#admin_authority").append(html);
        }
    }, function () {
        layer.msg("网络错误");
    }, false);
};

initTable.role = function () {
    $("#role_authority").empty();
    $("#role-authority").empty();
    myajax("/authority/getallauthority", "get", {}, function (result) {
        if (result.status == 200) {
            let html = '';
            for (let authority of result.data) {
                html += '<button type="button" class="layui-btn layui-btn-primary layui-btn-sm my-select-checkbox admin_authority" aid="' + authority.id + '" onclick="checkedShow(this)" value="' + authority.flag + '">' + authority.authority + '</button>';
            }
            $("#role_authority").append(html);
            $("#role-authority").append(html);
        }
    }, function () {
        layer.msg("网络错误");
    }, false);
};

function starttimechange(obj) {
    var endtime = $('#endtime').val();
    if (endtime != null && endtime != '' && $(obj).val() > endtime)
        $(obj).val(endtime);

}

function start_time_change(obj) {
    var end_time = $('#end_time').val();
    if (end_time != null && end_time != '' && $(obj).val() > end_time)
        $(obj).val(end_time);
}

function endtimechange(obj) {
    var starttime = $('#starttime').val();
    if (starttime != null && starttime != '' && $(obj).val() < starttime)
        $(obj).val(starttime);

}

function end_time_change(obj) {
    var start_time = $('#start_time').val();
    if (start_time != null && start_time != '' && $(obj).val() < start_time)
        $(obj).val(start_time);
}


function checkedShow(ele) {
    if ($(ele).attr('class').includes('admin_role'))
        $('.admin_authority').addClass('layui-btn-primary')
    if ($(ele).attr('class').includes('admin_authority'))
        $('.admin_role').addClass('layui-btn-primary')

    if ($(ele).attr('class').includes('layui-btn-primary'))
        $(ele).removeClass('layui-btn-primary');
    else
        $(ele).addClass('layui-btn-primary');

}

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


//==================================


//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


//监听 表头 工具栏 事件
function selectAndReset(id, obj) {
    switch (obj.event) {
        case 'reset':
            element.tabDelete("content-tab", id);//删除
            $("#vue-nav a[name='" + id + "']").trigger("click");
            break;


        case 'admincheckboxselect':
            var formdata = {};
            formdata = form.val("toolbar_form_" + id);
            let role_flag = 0;
            let roles = [];
            $('#admin_role').children('button').each(function () {
                if (!$(this).attr('class').includes('layui-btn-primary')) {
                    role_flag += parseInt($(this).val());
                    roles.push($(this).text());
                }
            });
            let authority_flag = 0;
            let authorities = [];
            $('#admin_authority').children('button').each(function () {
                if (!$(this).attr('class').includes('layui-btn-primary')) {
                    authority_flag += parseInt($(this).val());
                    authorities.push($(this).text());
                }
            });
            formdata.role_flag = role_flag;
            formdata.authority_flag = authority_flag;
            tableInss[id].reload({
                where: formdata,
                page: {
                    curr: 1
                }
            });
            form.val("toolbar_form_" + id, formdata);
            roles.forEach(function (val) {
                $('#admin_role').children('button').each(function () {
                    if ($(this).text() == val)
                        $(this).removeClass('layui-btn-primary');
                });
            });
            authorities.forEach(function (val) {
                $('#admin_authority').children('button').each(function () {
                    if ($(this).text() == val)
                        $(this).removeClass('layui-btn-primary');
                });
            });
            break;

        case 'rolecheckboxselect':
            var formdata = form.val("toolbar_form_" + id);

            let role_authority_flag = 0;
            let role_authorities = [];
            $('#role_authority').children('button').each(function () {
                if (!$(this).attr('class').includes('layui-btn-primary')) {
                    role_authority_flag += parseInt($(this).val());
                    role_authorities.push($(this).text());
                }
            });
            formdata.authority_flag = role_authority_flag;
            tableInss[id].reload({
                where: formdata,
                page: {
                    curr: 1
                }
            });
            form.val("toolbar_form_" + id, formdata);
            role_authorities.forEach(function (val) {
                $('#role_authority').children('button').each(function () {
                    if ($(this).text() == val)
                        $(this).removeClass('layui-btn-primary');
                });
            });
            break;


        case 'authorityselect':
            var formdata = form.val("toolbar_form_" + id);
            tableInss[id].reload({
                where: formdata,
                page: {
                    curr: 1
                }
            });
            form.val("toolbar_form_" + id, formdata);
            break;


        case 'selfreportselect':
            var formdata = form.val("toolbar_form_" + id);
            tableInss[id].reload({
                where: formdata,
                page: {
                    curr: 1
                }
            });
            form.val("toolbar_form_" + id, formdata);
            break;

        case 'createword':
            $.ajax({
                headers: {
                    "Authorization": localStorage.getItem("backstage_token")
                },
                url: "/dailyreport/downloadword",//提交的路径
                type: 'post',//提交方式
                contentType: "application/json",
                data: JSON.stringify(table.cache[id]),//查询的参数
                dataType: "json",//返回的类型  Text文本
                success: function (result, response, xhr) {
                    let token = xhr.getResponseHeader("Authorization");
                    if (token != null) {
                        localStorage.setItem("backstage_token", token);
                    }
                    if (result.status == 403) {
                        window.location.href = "/html/login.html";
                    } else {
                        if (result.status == 200)
                            layer.msg('导出成功');
                        else
                            layer.msg('服务器错误，导出失败');
                    }
                }
            });
            break;

        case 'wordselect':
            var formdata = form.val("toolbar_form_" + id);
            tableInss[id].reload({
                where: formdata,
                page: {
                    curr: 1
                }
            });
            form.val("toolbar_form_" + id, formdata);
            break;

        case 'mycollection':
            table.reload(id, {
                elem: "#" + id, //指定原始表格元素选择器（推荐id选择器）
                url: '/wxword/getmyword',
                response: {
                    statusName: 'status',//规定数据状态的字段名称，默认：code
                    statusCode: 200 //规定成功的状态码，默认：0
                },
                page: true,
                limit: 10,
                limits: [10, 15, 20, 25, 30, 35, 40],
                defaultToolbar: [],
                toolbar: "#table_options_toolbar_html_mycollection",
                cols: table_options.cols[id],
                id: id
                //,…… //更多参数参考右侧目录：基本参数选项
            });
            break;
    }
}


var tableToolBarform_insertsubmit = {};

tableToolBarform_insertsubmit.role = function () {
    form.on('submit(role_insert)', function () {
            var insertdata = form.val('toolbar_form_role');
            insertdata.authorityids = [];
            $('#role_authority').children('button').each(function () {
                if (!$(this).attr('class').includes('layui-btn-primary')) {
                    insertdata.authorityids.push($(this).attr('aid'));
                }
            });
            layer.msg('网络请求中...');
            myajax('/role/insertrole', 'post', insertdata,
                function (result) {
                    if (result.status == 200) {
                        tableInss['role'].reload();
                        layer.msg('新增成功');
                    } else if (result.status == 400) {
                        layer.msg('参数错误，新增失败');
                    } else if (result.status == 500) {
                        layer.msg('服务器错误，新增失败');
                    }
                },
                function () {
                    layer.msg("网络请求错误");
                });
            return false;
        }
    );
};

//===================================================
tableToolBarform_insertsubmit.authority = function () {
    form.on('submit(authority_insert)', function () {
        var formdata = form.val("toolbar_form_authority");
        layer.msg('网络请求中...');
        myajax('/authority/insertauthority', 'post', formdata,
            function (result) {
                if (result.status == 200) {
                    tableInss['authority'].reload();
                    layer.msg('新增成功');
                } else if (result.status == 400) {
                    layer.msg('参数错误，新增失败');
                } else if (result.status == 500) {
                    layer.msg('服务器错误，新增失败');
                }
            },
            function () {
                layer.msg("网络请求错误");
            });
        return false;
    })
};

//=========================================
tableToolBarform_insertsubmit.word = function () {
    form.on('submit(word_insert)', function () {
        var formdata = form.val("toolbar_form_word");
        layer.msg('网络请求中...');
        myajax('/wxword/add', 'post', formdata,
            function (result) {
                if (result.status == 200) {
                    tableInss['word'].reload();
                    layer.msg('新增成功');
                } else if (result.status == 400) {
                    layer.msg('参数错误，新增失败');
                } else if (result.status == 500) {
                    layer.msg('服务器错误，新增失败');
                }
            },
            function () {
                layer.msg("网络请求错误");
            });
        return false;
    })
};

//弹窗表单  修改模块
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


var rowToolBar_update_layerform = {
    "idselector": {},
    "area": {}
};

//==================================================

rowToolBar_update_layerform.idselector.admin = '#rowToolBar_update_layerform_html_admin';

rowToolBar_update_layerform.area.admin = ['800px', '700px'];


//===================================================
rowToolBar_update_layerform.idselector.role = '#rowToolBar_update_layerform_html_role';

rowToolBar_update_layerform.area.role = ['800px', '300px'];


//===================================================
rowToolBar_update_layerform.idselector.authority = '#rowToolBar_update_layerform_html_authority';

rowToolBar_update_layerform.area.authority = ['800px', '300px'];


//===================================================


//监听行工具条
function rowToolBar(id, obj) {
    switch (obj.event) {
        case 'user_update':
            layer.open({
                type: 1,
                title: "修改" + $("a[name=" + id + "]").text(),
                content: $(rowToolBar_update_layerform.idselector[id]),
                area: rowToolBar_update_layerform.area[id],
                resize: false,
                success: function (layero, index) {
                    let roles = obj.data.roles;
                    form.val(id + '_updateform', obj.data);
                    $('#admin-role>input[name=roles]').each(function () {
                        $(this).prop("checked", false);
                    });
                    roles.forEach(function (val) {
                        $('#ROLE_update_' + val).prop("checked", true);
                    });

                    authorityShow();

                    $("#admin-authority").empty();
                    $("#admin-authority-add").empty();
                    $("#admin-authority-del").empty();
                    myajax("/superadmin/getauthoritiesbyuserid", "get", {id: obj.data.id}, function (result) {
                        if (result.status == 200) {
                            if (result.data.residue.length > 0)
                                result.data.residue.forEach(function (val) {
                                    $("#admin-authority").append('<button  type="button" class="layui-btn layui-btn-sm user-authority" authority_id="' + val.id + '" >' + val.authority + '</button>')
                                });
                            if (result.data.add.length > 0)
                                result.data.add.forEach(function (val) {
                                    $("#admin-authority-add").append('<button  type="button" class="layui-btn layui-btn-sm user-authority" authority_id="' + val.id + '" >' + val.authority + '</button>')
                                });
                            if (result.data.del.length > 0)
                                result.data.del.forEach(function (val) {
                                    $("#admin-authority-del").append('<button  type="button" class="layui-btn layui-btn-sm user-authority" authority_id="' + val.id + '" >' + val.authority + '</button>')
                                });
                        }
                    }, null, false);

                    form.render();

                    $('.user-authority').draggable({
                        revert: true
                    });
                    $('.authority-drop').droppable({
                        onDrop: function (e, source) {
                            $(this).append(source);
                            form.render();
                        }
                    });

                    form.on('checkbox(admin_update)', function () {
                        authorityShow();
                    });

                    form.on('submit(' + id + '_update)', function () {
                            var updatedata = {};
                            updatedata.id = obj.data.id;

                            let roles_id = [];
                            $('#admin-role>input[name=roles]:checked').each(function () {
                                roles_id.push($(this).attr("role_id"));
                            });
                            updatedata.roleids = roles_id;

                            updatedata.addAuthorities = [];
                            updatedata.delAuthorities = [];
                            var adds = $("#admin-authority-add").children('button');
                            var dels = $("#admin-authority-del").children('button');
                            if (adds.length > 0)
                                adds.each(function () {
                                    updatedata.addAuthorities.push($(this).attr("authority_id"));
                                });
                            if (dels.length > 0)
                                dels.each(function () {
                                    updatedata.delAuthorities.push($(this).attr("authority_id"));
                                });
                            layer.msg('网络请求中...');
                            myajax('/superadmin/updateuser', "post", updatedata, function (result) {
                                if (result.status == 200) {
                                    tableInss[id].reload();
                                    layer.close(index);
                                    layer.msg('修改成功');
                                } else {
                                    layer.msg('修改失败');
                                }
                            }, function () {
                                layer.msg('网络错误，修改失败');
                            });
                            return false;
                        }
                    );
                }
            });
            break;
        case 'role_update':
            layer.open({
                type: 1,
                title: "修改" + $("a[name=" + id + "]").text(),
                content: $(rowToolBar_update_layerform.idselector[id]),
                area: rowToolBar_update_layerform.area[id],
                resize: false,
                success: function (layero, index) {
                    let authorities = obj.data.authorities;
                    form.val(id + '_updateform', obj.data);
                    $('#role-authority').children('button').each(function () {
                        $(this).addClass('layui-btn-primary');
                    });
                    authorities.forEach(function (val) {
                        $('#role-authority').children('button').each(function () {
                            if ($(this).text() == val)
                                $(this).removeClass('layui-btn-primary');
                        });
                    });
                    form.on('submit(' + id + '_update)', function () {
                        var updatedata = form.val(id + '_updateform');
                        updatedata.id = obj.data.id;
                        updatedata.authorityids = [];
                        $('#role-authority').children('button').each(function () {
                            if (!$(this).attr('class').includes('layui-btn-primary')) {
                                updatedata.authorityids.push($(this).attr('aid'));
                            }
                        });
                        layer.msg('网络请求中...');
                        myajax('/role/updaterole', 'post', updatedata, function (result) {
                            if (result.status == 200) {
                                tableInss[id].reload();
                                layer.close(index);
                                layer.msg('修改成功');
                            } else {
                                layer.msg('修改失败');
                            }
                        }, function () {
                            layer.msg('网络错误，修改失败');
                        });
                        return false;
                    });
                }
            });
            break;
        case 'role_delete':
            myajax('/role/deleterole', 'post', {id: obj.data.id}, function (result) {
                if (result.status == 200) {
                    tableInss[id].reload();
                    layer.msg('删除成功');
                } else {
                    layer.msg('删除失败');
                }
            }, function () {
                layer.msg('网络错误，删除失败');
            });
            break;
        case 'authority_update':
            layer.open({
                type: 1,
                title: "修改" + $("a[name=" + id + "]").text(),
                content: $(rowToolBar_update_layerform.idselector[id]),
                area: rowToolBar_update_layerform.area[id],
                resize: false,
                success: function (layero, index) {
                    form.val(id + '_updateform', obj.data);
                    form.on('submit(' + id + '_update)', function () {
                        var updatedata = form.val(id + '_updateform');
                        updatedata.id = obj.data.id;
                        layer.msg('网络请求中...');
                        myajax('/authority/updateauthority', 'post', updatedata, function (result) {
                            if (result.status == 200) {
                                tableInss[id].reload();
                                layer.close(index);
                                layer.msg('修改成功');
                            } else {
                                layer.msg('修改失败');
                            }
                        }, function () {
                            layer.msg('网络错误，修改失败');
                        });
                        return false;
                    });
                }
            });
            break;
        case 'authority_delete':
            layer.msg('网络请求中...');
            myajax('/authority/deleteauthority', 'post', {id: obj.data.id}, function (result) {
                if (result.status == 200) {
                    tableInss[id].reload();
                    layer.msg('删除成功');
                } else {
                    layer.msg('删除失败');
                }
            }, function () {
                layer.msg('网络错误，删除失败');
            });
            break;

        case 'selfreport_show':
            if (obj.data.status == 0) {
                layer.open({
                    type: 1,
                    title: false,
                    content: $('#view_data_insertdailyreport__'),
                    area: ['800px', '100%'],
                    resize: false,
                    closeBtn: 0,
                    btn: ['保存', '提交', '退出'],
                    btn1: function (index, layero) {
                        addreport(index, 0, obj.data);
                        tableInss['selfreport'].reload();
                    },
                    btn2: function (index, layero) {
                        addreport(index, 1, obj.data);
                        tableInss['selfreport'].reload();
                        return false;
                    },
                    btn3: function (index, layero) {
                        element.tabDelete("content-tab", 'insertdailyreport__');
                    },
                    success: function (layero, index) {
                        initselect(layero, index, obj.data);
                    }
                });
            } else if (obj.data.status == 1) {
                layer.open({
                    type: 1,
                    title: false,
                    content: $('#view_data_insertdailyreport__'),
                    area: ['800px', '100%'],
                    resize: false,
                    closeBtn: 0,
                    btn: ['保存', '退出'],
                    btn1: function (index, layero) {
                        addreport(index, 1, obj.data);
                    },
                    btn2: function (index, layero) {
                        element.tabDelete("content-tab", 'insertdailyreport__');
                    },
                    success: function (layero, index) {
                        initselect(layero, index, obj.data);
                    }
                });
            } else {
                layer.open({
                    type: 1,
                    title: false,
                    content: $('#view_data_insertdailyreport__'),
                    area: ['800px', '100%'],
                    resize: false,
                    success: function (layero, index) {
                        initselect(layero, index, obj.data);
                    }
                });
            }
            break;

        case 'adminreport_show':
            layer.open({
                type: 1,
                title: false,
                content: $('#view_data_insertdailyreport__'),
                area: ['800px', '100%'],
                resize: false,
                closeBtn: 0,
                btn: ['退出'],
                yes: function (index, layero) {
                    if (obj.data.status == 1) {
                        myajax('/dailyreport/checkreport', 'post', {id: obj.data.id}, function (res) {
                            if (res.status == 200) {
                                tableInss['adminreport'].reload();
                            }
                        }, function () {
                            layer.msg("网络错误");
                        });
                    }
                    layer.close(index);
                },
                success: function (layero, index) {
                    initselect(layero, index, obj.data);
                }
            });
            break;
    }
}

function addreport(index, status, data) {
    var insertdata = {};
    if (data != null) {
        insertdata = data;
    }
    insertdata.userClass = $('#userclass').val();
    insertdata.classTeacherName = $('#classTeacher').val();
    insertdata.teacherName = $('#teacher').val();
    insertdata.report = '';
    for (let i = 1; i <= 4; i++) {
        var value = $("#reportcontent" + i).val();
        if (value == null || value == '') {
            value = KONG;
        }
        insertdata.report += CONTENTSPLIT + value;
    }
    insertdata.report = insertdata.report.substring(CONTENTSPLIT.length);
    insertdata.status = status;
    layer.msg('网络请求中...');
    myajax('/dailyreport/save', 'post', insertdata, function (res) {
        if (res.status == 200) {
            layer.msg('提交成功');
            layer.close(index);
            element.tabDelete("content-tab", 'insertdailyreport__');
        } else {
            layer.msg('提交失败');
        }
    }, function () {
        layer.msg('网络错误，提交失败');
    });
}


function authorityShow() {
    let roles_id = [];
    $('#admin-role>input[name=roles]:checked').each(function () {
        roles_id.push($(this).attr("role_id"));
    });
    myajax("/authority/getauthoritybyroleids", "get", {ids: roles_id}, function (res) {
        if (res.status == 200) {
            $("#user_update_show_authority").empty();
            if (res.data != null)
                res.data.forEach(function (val) {
                    $("#user_update_show_authority").append('<div style="margin: 5px" class="layui-btn layui-btn-sm">' + val + '</div>');
                });
        }
    }, function () {
        layer.msg("网络错误");
    });
}


//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

var load_view_data = {};
var CONTENTSPLIT = "!@#$%^&*!";
var KONG = "_#!_$@";

//================================

load_view_data.insertdailyreport__ = function () {
    layer.open({
        type: 1,
        title: false,
        content: $('#view_data_insertdailyreport__'),
        area: ['800px', '100%'],
        resize: false,
        closeBtn: 0,
        btn: ['存草稿', '提交', '退出'],
        btn1: function (index, layero) {
            addreport(index, 0);
            if (tableInss['selfreport'] != null)
                tableInss['selfreport'].reload();
        },
        btn2: function (index, layero) {
            addreport(index, 1);
            tableInss['selfreport'].reload();
            return false;
        },
        btn3: function (index, layero) {
            element.tabDelete("content-tab", 'insertdailyreport__');
        },
        success: function (layero, index) {
            initselect(layero, index);
        }
    });
};

//初始化下拉框
function initselect(layero, index, data) {
    myajax('/user/getuserclassinfo', 'get', {}, function (res) {
        if (res.status == 200) {
            var classhtml = '';
            if (res.data.userRoles.length > 0)
                res.data.userRoles.forEach(function (val) {
                    classhtml += '<option value="' + val + '">' + val + '</option>';
                });
            $('#userclass').html(classhtml);
            var teacherhtml = '';
            if (res.data.teacher.length > 0)
                res.data.teacher.forEach(function (val) {
                    teacherhtml += '<option value="' + val.name + '">' + val.name + '</option>';
                });
            $('#teacher').html(teacherhtml);
            var classTeacherhtml = '';
            if (res.data.classTeacher.length > 0)
                res.data.classTeacher.forEach(function (val) {
                    classTeacherhtml += '<option value="' + val.name + '">' + val.name + '</option>';
                });
            $('#classTeacher').html(classTeacherhtml);
            if (data != null) {
                $('#userclass').val(data.userClass);
                $('#teacher').val(data.teacherName);
                $('#classTeacher').val(data.classTeacherName);
                myajax('/report/getreportbyid', 'get', {id: data.dailyId}, function (res) {
                    if (res.status == 200) {
                        if (res.data != null) {
                            var coontentdata = res.data.split(CONTENTSPLIT);
                            for (let i = 1; i <= 4; i++) {
                                if (coontentdata[i - 1] != KONG)
                                    $("#reportcontent" + i).val(coontentdata[i - 1]);
                            }
                        }
                    }
                }, function () {
                    layer.msg("网络错误");
                });
            }
        }
    });
}
